package com.atguigu.yygh.user.controller.admin;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.vo.user.UserInfoQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "前台的后端用户模块")
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("用户列表,条件查询带分页")
    @GetMapping("/list/{page}/{limit}")
    public Result findUserInfoList(@PathVariable("page") Long page,
                                   @PathVariable("limit") Long limit,
                                   UserInfoQueryVo userInfoQueryVo) {

        // 调用service查询分页方法
        IPage<UserInfo> UserInfoPageMode = userInfoService.findUserInfoListPage(page, limit, userInfoQueryVo);
        // 返回分页数据
        return Result.ok(UserInfoPageMode);
    }

    @ApiOperation("用户的锁定和解除")
    @GetMapping("/lock/{userInfoId}/{status}")
    public Result lockUserInfo(@PathVariable("userInfoId") Long userInfoId,
                               @PathVariable("status") Integer status) {
        // 调用service根据id查询用户对象
        UserInfo userInfo = userInfoService.getById(userInfoId);
        if (ObjectUtils.isEmpty(userInfo)) {
            return Result.fail();
        }

        // 更新用户的状态  '状态（0：锁定 1：正常）'
        if (0 == status || 1 == status) {
            userInfo.setStatus(status);
            userInfoService.updateById(userInfo);
        }

        return Result.ok();
    }

    @ApiOperation("通过用户Id查看详情")
    @GetMapping("/show/{userInfoId}")
    public Result show(@PathVariable("userInfoId") Long userInfoId) {
        Map<String, Object> resultMap = userInfoService.show(userInfoId);
        return Result.ok(resultMap);
    }

    @ApiOperation("认证审批")
    @GetMapping("/approval/{userInfoId}/{authStatus}")
    public Result approval(@PathVariable("userInfoId") Long userInfoId,
                           @PathVariable("authStatus")Integer authStatus){

        userInfoService.approval(userInfoId,authStatus);

        return Result.ok();
    }



}
